home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / XINE-1.ZIP / XINE-1.009 < prev    next >
Text File  |  1996-10-25  |  10KB  |  189 lines

  1.  
  2.                                         /-----------------------------\
  3.                                         | Xine - issue #1 - Phile 009 |
  4.                                         \-----------------------------/
  5.  
  6.  
  7.                         Mutation Engines
  8.                             by JHB
  9.  
  10.  
  11.   Since I first heard about the idea of a Mutation engine I was
  12. interested to see the code that would explain how such a devious piece of
  13. code could work.  I had taken apart several viruses, but most mutation
  14. engines due to thier nature are difficult to dissassemble.  So after
  15. seeing other people's code I decide to try my hand at this type of coding.
  16. This article will illustrate the path I took in designing and building a
  17. Mutation Engine.
  18.   To start I will try to define what a Mutation engine is, its code that
  19. can be link or added to a regular program or virus that will:
  20.  
  21.  1 Encrypt itself as well as the program that is linked to it
  22.  2 Create a Decryptor that will be run first before the main program
  23.  3 Each Decryptor that it creates has a different signature
  24.  
  25.  Ok, now lets explain the defination, a signature (when refering to code)
  26. is a piece of code that should be unique to that code. An example of this
  27. is the signature:
  28.         EA 05 00 C0 07
  29. Which is the hex code for
  30.         jmp     07c0:0005
  31.   This (which some may reconize) is the first line of code for the Stoned
  32. virus though it is short piece of code, placing this piece of code in the
  33. first part of the boot sector use to cause many Av products to false
  34. alarm. (not tested lately)
  35.   Now a true mutation engine could alter this code to some other code
  36. that would accomplish the same thing, though this is possible it would
  37. make the engine huge.  Mutation Engine writers have taken a easier way out
  38. they encrypt the main code and the engine then put a decryptor as the 
  39. first thing that is run. 
  40.  This means that the only signature that has to alter is the decryptor,
  41. this is the main type of engine that is created these days.
  42.  
  43.   The first part of the creation process is to create a
  44. encryptor/decryptor that this should be fairly simply encrytion.  I played
  45. with the standard xor/add/sub but they were spotted immeditaly by TBAV and
  46. F-prot as encrypted code. Since the main idea of a mutation engine is to
  47. fool both people and scanners this was not a good start.  Well while
  48. reading the usenet group I saw a mention of a encryption that uses
  49. shl/shr.  Many people may wonder if you use a shl/shr the leading bit or 
  50. end bit is lost this is not true it is moved the carry flag.  By simply
  51. adding this to the front or end of the register (or word) you can have
  52. reversible encrytion. Now TBAV will find most of these except for even
  53. number shifts like 2,4,8  while F-prot seems to miss most of these( only
  54. in heurstices and the /parnoid). (I was told this is because the shl 2,4,8
  55. code can also be used as a quick multiply or divide, so tbav will not flag
  56. this encryption). So I soon had this as a decryption module
  57.                 mov bx,offset virus_code
  58. decrypt:
  59.                 mov     cx,4              ;encrytion key
  60.                 mov     ax,cs:[bx]        ;get the word to encrypt
  61. again:
  62.                 clc                       ;not needed I was young forgive me ;)
  63.                 shl     1                 ;ok so ax before this code is
  64.                                           ; ax 1000 0000 0000 0000 cf = 0
  65.                                           ;after the shl
  66.                                           ; ax 0000 0000 0000 0000 cf = 1
  67.                 jnc     no_high_bit       ;
  68.                 inc     ax                ;this simpy makes ax =
  69.                                           ; ax 0000 0000 0000 0001
  70. no_high_bit:
  71.                 dec     cx
  72.                 jcxz    done_unencr       ;ok done shifting it again
  73.                 jmp     short again       ;shift it again
  74. done_enencr:
  75.                 mov     cs:[bx],ax        ;Store the decrypted code
  76.                 add     bx,2              ; move our pointer
  77.                 dec     dx                      ;track how much is left
  78.                 jnz     decrypt
  79.  
  80. virus_code:
  81.  
  82.   Ok, so there we have a decryptor now use a shr and you have the
  83. encryption routine (its in the code read it ;) ).
  84.    Ok now at this point I have a encryption engine that will fool TBAV
  85. and F-prot ( unless you use the f-prot /paranoid which the average user
  86. just does not do) but this is not a mutation engine.  Well one simple
  87. mutation is to change the registers its a simple thing just requires some
  88. research that debug or a decent assembly book will show.  All assemble
  89. instructions are built in a pattern take
  90.                         push    ax              which in hex is
  91.                         50h  or 1001 0000b
  92.                                                 now
  93.                         push    bx              is
  94.                         53h  or 10010 011b
  95.  
  96. note that the 3 last bits are they only thing that changes this follows
  97. through so that for the word registers
  98.                         ax = 000  0
  99.                         cx = 001  1
  100.                         dx = 010  2
  101.                         bx = 011  3
  102.                         sp = 100  4
  103.                         bp = 101  5
  104.                         si = 110  6
  105.                         di = 111  7
  106.   Ok so with some simple and/or instruction  we can change the registers
  107. any other register of course we must not just randomly change registers
  108. we have to track which register we have use and make sure that we change
  109. all bx to cx.
  110.  This is where some people start to get lost because we start treating
  111. code we are going to run as data. Take this example:
  112.                 
  113.                 mov     cx,0004         ;b90400h        
  114. becomes
  115.  
  116. shift_reg:
  117.                 db      0b9h            ;10111 001  <- cx
  118.                 dw      0004
  119.  
  120. Now the mutation for this code might look like this
  121.  
  122.                 mov     ax,[shift_reg]    ;
  123.                 and     ax,11111000h      ;this will isolate the mov
  124.                                           ;instruction
  125.                 or      ax,00000111       ;this would make the instruction
  126.                                           ;mov di
  127. A better way would be to use a random number generator and get a number
  128. between 0-7 then OR that into it rather than the constant I used. Of
  129. course you must track what registers you use as you go.
  130.  
  131.  Now using this technique we have a limited mutation engine and just so
  132. you do not forget F-prot still catches it. So the next simple mutation use
  133. a sorta undocumented form of the shl instruction.
  134.  
  135.         shl AX,1        D1 E0    1101 000 1 1110 0000 <-USUAL CODE
  136.         SHL AX,1        D1 F0    1101 111 1 1110 0000 <-UNDOC 
  137.                
  138.         SHL AX,1        C1 E0 01                      <-USUAL CODE
  139.         SHL AX,1        C1 F0 01                      <-UNDOC
  140.    
  141.   
  142.         D1 F0           <-APPEARS THAT NEITHER FP OR TB FIND IT
  143.  
  144.  Now the unuasual part while all of the above code does the same thing
  145. shift the bits in the AX reg left the ones with the F0h are not used by
  146. many assemblers. (Though I have read that a shareware Assembler may use
  147. these codes maybe to track code that was produced by it).  Using the
  148. "undocumented" form will fool f-prot every time but the c1 f0 01 will flag
  149. tbav with (and I love this flag) " 1 found instruction that will require
  150. an 80186 processor or above" Like they have add no instructions to the
  151. code set after the 8086!! But for a virus generated code this would not be
  152. a flag you want.  Since any flag that was not there before will cause a
  153. problem.
  154.  
  155.   Ok so we have a simple mutation a reversible simple encryption and at
  156. least 4 forms of the decryptor code not including the mutation of the
  157. registers.  The next part is to add do nothing or noise code.  This is
  158. where careful creativity comes into play.  Noise instructions or Do
  159. nothing bytes are instructions that actauly do, do something they just do
  160. not affect your code. NOP, XCHG AX,AX are some examples of do nothing
  161. code.  In the early days of mutation engines and herustic scanners just
  162. adding nops and xchg code could make the virus unscannable.  But as
  163. scanners improved they now will watch for such do nothing code, either
  164. ignoring it and seeing the real code or warning the user that there is
  165. code that serves no purpose but to hide a virus.  Again I found that you
  166. could use some of these ideas say 1 or two nops in a row but too many and
  167. TBAV went to red alert. So I thought instead of using just one byte
  168. instructions why not have routines that called interrupts.  just find
  169. ones that do not affect to many regs or put push regs code pop regs this
  170. way none of the real code is affected. An Example
  171.  
  172. noise_6:        
  173.         push    ax           ;1    get the shift flags only affects the
  174.         mov     ax,0200h     ;3    ax reg so push it then pop it.
  175.     int     16h          ;2
  176.     pop     ax           ;1
  177. end_noise_6:
  178.     
  179.  
  180.   Combine all these ideas togther and you can have an interesting piece
  181. of code that will produce several differnt variations on the decryptor. 
  182. Well thats about all for me. If your realy interested in fooling with
  183. making a mutation engine get ahold of of as many source codes as you can
  184. and examine them then attempt one on your own.  Even if you fail in
  185. making a viable mutation engine you will understand them far better.  I
  186. did not bother to make my code a true engine its an example thats all.
  187.  
  188.  
  189.